// pages/zhaobiao/zhaobiao.js
var app = getApp()
var util = require("../../utils/util.js");
var config = require("../../utils/config.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    screen: app.data.screen,
    hidden: 2,
    isRefresh: false,
    bottom: true,
    page: 1,
    message: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          that.request();
        } else {
          wx.navigateTo({
            url: '/pages/login/login',
          });
        }
      }
    })
  },
  onShow() {

  },
  toDeatil(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/caigouDetail/caigouDetail?id=' + id,
    })
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh() {
    this.data.page = 1;
    this.data.isRefresh = true;
    this.data.bottom = true
    this.request();
    wx.stopPullDownRefresh(); //防止真机不弹回 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.bottom == true && this.data.message.length > 0) {
      this.request();
    }
  },
  /**
     * 网络请求
     */
  request() {
    util.getOpenID((e) => {
      var parameters = {
        openid: e.openid,
        page: this.data.page
      }
      util.postRequest(config.project, parameters, (res) => {
        console.log(res);
        if (res.code == 200) {
          var listmes = res.data.project_list;
          var datalist = this.data.message;
          if (listmes.length == 0 && datalist.length == 0) {
            this.setData({
              hidden: 0,
              bottom: false,
            })
          } else {
            // 下拉刷新
            if (this.data.isRefresh == true) {
              this.data.isRefresh = false,
              datalist = [];
              wx.stopPullDownRefresh();
            }
            // 上拉加载 追加数据
            if (datalist.length == 0 || datalist == 'undefined') {
              datalist = listmes;
            } else {
              datalist = datalist.concat(listmes);
              // console.log(datalist);
              if (listmes.length < 2) {
                this.setData({
                  bottom: false
                })
              }
            }
            this.setData({
              message: datalist,
              hidden: 1,
              page: this.data.page + 1,
            })
          }
        } else {
          wx.showToast({ title: '请求失败!', icon: 'none' })
          this.setData({
            hidden: 0,
          })
        }
      }, (e) => {
        this.setData({
          hidden: 0,
        })
      }, () => {
        //请求完成
      }, false);
    }, false);
  },

})